package com.briup.es.web;

import com.briup.es.config.response.Result;
import com.briup.es.domain.dto.BookChangeNumDTO;
import com.briup.es.domain.vo.BookDetailVO;
import com.briup.es.domain.vo.BookVO;
import com.briup.es.service.BookService;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    /**
     * 首页展示图书信息 只展示20本
     *
     * @return 图书信息
     */
    @GetMapping("/showBooks")
    public Result<List<BookVO>> showBooks() {
        List<BookVO> books = bookService.showBooksLimit20();
        return Result.success(books);
    }

    @GetMapping("/findByCategory/{id}")
    public Result<List<BookVO>> findByCategory(@PathVariable
                                               @Min(value = 1, message = "必须保证栏目id是存在的") Integer id) {
        List<BookVO> books = bookService.findByCategoryId(id);
        return Result.success(books);
    }

    @GetMapping("/searchBook")
    public Result<List<BookVO>> searchBook(
            @NotBlank(message = "关键词不能为空") String query) {
        List<BookVO> books = bookService.searchBook(query);
        return Result.success(books);
    }

    @GetMapping("/{id}")
    public Result<BookDetailVO> queryBookDetail(@PathVariable
                                                @Min(value = 1, message = "必须保证图书id是存在的") Integer id) {
        BookDetailVO detailVO = bookService.queryBookDetailById(id);
        return Result.success(detailVO);
    }

    @PostMapping("/queryByIds")
    public Result<List<BookVO>> queryByIds(@RequestBody List<Integer> ids) {
        List<BookVO> books = bookService.queryByIds(ids);
        return Result.success(books);
    }

    @GetMapping("/queryById")
    public Result<BookVO> queryById(Integer id) {
        BookVO book = bookService.queryById(id);
        return Result.success(book);
    }

    @PutMapping("/updateBookNum")
    public Result<?> updateBookNum(@RequestBody List<BookChangeNumDTO> dtoList){
        bookService.changeStoreNum(dtoList);
        return Result.success();
    }
}
